package org.jeecg.modules.consumptionSteam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.consumptionSteam.entity.ConsumptionSteam;
import org.jeecg.modules.consumptionSteam.entity.SteamSelectDTO;
import org.jeecg.modules.consumptionSteam.mapper.ConsumptionSteamMapper;
import org.jeecg.modules.consumptionSteam.service.IConsumptionSteamService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 水蒸汽能耗
 * @Author: jeecg-boot
 * @Date: 2023-06-20
 * @Version: V1.0
 */
@Service
@Slf4j
public class ConsumptionSteamServiceImpl extends ServiceImpl<ConsumptionSteamMapper, ConsumptionSteam> implements IConsumptionSteamService {

    @Override
    public List<ConsumptionSteam> selectByTimeAndDept(SteamSelectDTO selectDTO) {
        if (selectDTO.getStartTime() == null || selectDTO.getEndTime() == null) {
            throw new JeecgBootException("服务器忙,请稍后再试...");
        }
        Date start = Date.from(selectDTO.getStartTime().toInstant());
        Date end = Date.from(selectDTO.getEndTime().toInstant());
        String paramS = DateUtils.formatDate(start, "yyyyMMdd");
        String paramE = DateUtils.formatDate(end, "yyyyMMdd");
        int s = Integer.parseInt(paramS);
        int e = Integer.parseInt(paramE);
        //log.info("起始时间: {}, 结束时间: {}", s, e);
        QueryWrapper<ConsumptionSteam> wrapper = new QueryWrapper<>();
        if ("date".equals(selectDTO.getDatePickerType())) {
            wrapper.between("date", s, e)
                    .eq("dept", selectDTO.getDept())
                    .orderByAsc("date");
            List<ConsumptionSteam> list = baseMapper.selectList(wrapper);
            return list;
        } else {
            wrapper.select("sum(consumption) as consumption", "year", "month")
                    .between("date", s, e)
                    .groupBy("month", "year").having("consumption > 0").orderByAsc("year*1", "month*1");
            return baseMapper.selectList(wrapper);
        }
    }

    @Override
    public List<Map<String, Object>> getAllGroupByDept() {
        QueryWrapper<ConsumptionSteam> wrapper = new QueryWrapper<>();
        wrapper.select("sum(consumption) as sum_value", "dept").groupBy("dept").having("sum_value > 0");
        return baseMapper.selectMaps(wrapper);
    }

    /**
     * 获取某天的能耗数据
     *
     * @param year  年
     * @param month 月
     * @param day   日
     * @return
     */
    @Override
    public List<Map<String, Object>> getDayConsumption(String year, String month, String day) {
        QueryWrapper<ConsumptionSteam> wrapper = new QueryWrapper<>();
        wrapper.select("consumption", "dept").eq("year", year).eq("month", month).eq("day", day);
        return baseMapper.selectMaps(wrapper);
    }

}
